﻿using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using SecuritySpace.ProtoType;
using SmartPeople.Framework.SecuritySpace;

namespace SecuritySpace
{
    public partial class Users
    {
        private ISecuritySpaceProviderFactory securitySpaceProviderFactory = new XPOProviderFactory();

        public Users()
        {
            InitializeComponent();

            // Insert code required on object creation below this point.
        }

        private void cmdCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void cmdSave_Click(object sender, RoutedEventArgs e)
        {
            ISecuritySpace securitySpace = securitySpaceProviderFactory.GetSecuritySpace();
            string fullname = txtFullName.Text;
            string email = txtEmail.Text;
            string username = txtUserName.Text;
            string password = txtPassword.Password;
            string confirmpassword = txtConfirmPassword.Password;
            string secretquestion = "";
            string passwordanswer = txtPasswordAnswer.Text;
            string comment = txtComment.Text;
            string description = txtDescription.Text;

            if (!ValidateUser(fullname, email, password, confirmpassword, passwordanswer, comment, description))
                return;


            bool approved = true;
            bool islockedout = false;
            securitySpace.Users.CreateUser(username, password, email, secretquestion, passwordanswer, approved, comment,
                                           description, islockedout);
        }


        private bool ValidateUser(string fullName, string emailAddress, string password, string confirmPassword
                                  , string passwordAnswer, string comment, string description)
        {
            if (string.IsNullOrEmpty(fullName))
            {
                MessageBox.Show("User Full Name is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }

            if (string.IsNullOrEmpty(fullName))
            {
                MessageBox.Show("User email is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }

            if (!ValidateEmail(emailAddress))
            {
                return false;
            }

            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("User password is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }

            if (string.IsNullOrEmpty(confirmPassword))
            {
                MessageBox.Show("User confirmation password is required!", "Application Information",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }

            if (string.IsNullOrEmpty((password != confirmPassword).ToString()))
            {
                MessageBox.Show("Password and Confirmation password do not match!", "Application Information",
                                MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }

            if (string.IsNullOrEmpty(passwordAnswer))
            {
                MessageBox.Show("User password answer is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }


            if (string.IsNullOrEmpty(comment))
            {
                MessageBox.Show("User comment  is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }


            if (string.IsNullOrEmpty(description))
            {
                MessageBox.Show("User description is required!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        private bool ValidateEmail(string email)
        {
            string pattern =
                @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$";
            Match match = Regex.Match(email.Trim(), pattern, RegexOptions.IgnoreCase);
            if (!match.Success)
            {
                //GetType().FullName
                //throw new ArgumentException(email + "is not a valid email id!");
                MessageBox.Show(email + "is not a valid email id!", "Application Information", MessageBoxButton.OK,
                                MessageBoxImage.Error);
                return false;
            }
            return true;
        }

        private void cmdCheck_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(cbxPasswordQuestion.SelectedItem.ToString());

            MessageBox.Show(cbxPasswordQuestion.SelectedValue.ToString());
            
        }
    }
}